package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import pojo.Notice;
import service.NoticeService;

import java.util.List;

@Api(tags = "Notice 通知接口")
@RestController
@RequestMapping("/notice")
@CrossOrigin
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    // 查询所有通知
    @ApiOperation("查询所有通知")
    @GetMapping("/findAll")
    public List<Notice> findAllNotices() {
        return noticeService.findAllNotices();
    }

    // 根据ID查询通知
    @ApiOperation("根据ID查询通知")
    @GetMapping("/findById/{id}")
    public Notice findNoticeById(@PathVariable Integer id) {
        return noticeService.findNoticeById(id);
    }

    // 根据标题模糊查询通知
    @ApiOperation("根据标题模糊查询通知")
    @GetMapping("/findByTitle")
    public List<Notice> findNoticesByTitle(@RequestParam String title) {
        return noticeService.findNoticesByTitle(title);
    }

    // 新增通知
    @ApiOperation("新增通知")
    @PostMapping("/add")
    public String addNotice(@RequestBody Notice notice) {
        int result = noticeService.addNotice(notice);
        return result > 0 ? "添加成功" : "添加失败";
    }

    // 修改通知
    @ApiOperation("修改通知")
    @PutMapping("/update")
    public String updateNotice(@RequestBody Notice notice) {
        int result = noticeService.updateNotice(notice);
        return result > 0 ? "修改成功" : "修改失败";
    }

    // 删除通知
    @ApiOperation("删除通知")
    @DeleteMapping("/delete/{id}")
    public String deleteNotice(@PathVariable Integer id) {
        int result = noticeService.deleteNotice(id);
        return result > 0 ? "删除成功" : "删除失败";
    }
}
